//
//  PostDetailToolBar.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/25.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "PostDetailToolBar.h"
#import "Masonry.h"
#import "PostsDetailViewModel.h"
#import "HomeChannelDetailModel.h"
#import "HomeChannelDetailContent.h"
#import "TAMacro.h"
@interface PostDetailToolBar ()

@property (nonatomic, weak) UIButton *heartButton;
@property (nonatomic, weak) UILabel *likedNumLabel;
@property (nonatomic, weak) UIButton *commentButton;
@property (nonatomic, weak) UILabel *commentNumLabel;
@property (nonatomic, weak) UIButton *moreButton;

@end



@implementation PostDetailToolBar

- (void)setPostDetailViewModel:(PostsDetailViewModel *)postDetailViewModel {
    _postDetailViewModel = postDetailViewModel;
    [self setUpData];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        // 添加子控件
        [self setUpChildrenView];
        [self setUpFrame];
        
    }
    return self;
}

// 创建子控件
- (void)setUpChildrenView {
    
    UIButton *heartButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    heartButton.backgroundColor = [UIColor redColor];
    [heartButton setImage:[UIImage imageNamed:@"heart"] forState:UIControlStateNormal];
    [heartButton addTarget:self action:@selector(heartButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:heartButton];
    _heartButton = heartButton;
    
    UILabel *likedNumLabel = [[UILabel alloc] init];
//    likedNumLabel.backgroundColor = [UIColor yellowColor];
    likedNumLabel.font = [UIFont systemFontOfSize:12];
    likedNumLabel.textColor = [UIColor lightGrayColor];
    [self addSubview:likedNumLabel];
    _likedNumLabel = likedNumLabel;
    
   
    
    
    UILabel *commentNumLabel = [[UILabel alloc] init];
//    commentNumLabel.backgroundColor = [UIColor yellowColor];
    commentNumLabel.font = [UIFont systemFontOfSize:12];
    commentNumLabel.textColor = [UIColor lightGrayColor];
    
    
    [self addSubview:commentNumLabel];
    _commentNumLabel = commentNumLabel;
    
   
    
    
    UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [commentButton setImage:[UIImage imageNamed:@"channelComment"] forState:UIControlStateNormal];
    [commentButton addTarget:self action:@selector(commentButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:commentButton];
    _commentButton = commentButton;
    
   
    // 分享按钮
    UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreButton setImage:[UIImage imageNamed:@"channelMore"] forState:UIControlStateNormal];
    [moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:moreButton];
    _moreButton = moreButton;


}

- (void)setUpFrame {
    
    [_moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
        make.right.mas_equalTo(-10);
        make.centerY.mas_equalTo(self.mas_centerY);
        
    }];
    
    [_commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.centerX.mas_equalTo(self.mas_centerX).multipliedBy(1.5);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    
    [_commentNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(20);
        make.left.mas_equalTo(self.commentButton.mas_right).mas_offset(2);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    [_heartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.centerX.mas_equalTo(self.mas_centerX).multipliedBy(1.25);
        
    }];
    
    [_likedNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(20);
        make.left.mas_equalTo(self.heartButton.mas_right).mas_offset(2);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
}



- (void)setUpData {
    


    if (_postDetailViewModel.homeChannelDetailModel.content.likeNum != 0) {
        _likedNumLabel.text = [NSString stringWithFormat:@"%ld", _postDetailViewModel.homeChannelDetailModel.content.likeNum];

    } else {
        _likedNumLabel.text = @"";
    }
    if (_postDetailViewModel.homeChannelDetailModel.content.commentNum != 0) {
        _commentNumLabel.text = [NSString stringWithFormat:@"%ld", _postDetailViewModel.homeChannelDetailModel.content.commentNum];

        
    } else {
        _commentNumLabel.text = @"";
    }
    if (_postDetailViewModel.homeChannelDetailModel.content.isLiked == YES) {
        [_heartButton setImage:[UIImage imageNamed:@"liked"] forState:UIControlStateNormal];
    } else {
        [_heartButton setImage:[UIImage imageNamed:@"heart"] forState:UIControlStateNormal];
    }
    
}


- (void)heartButtonAction:(UIButton *)button {
    [self.likeDelegate clickLikeButton:button];
}

- (void)moreButtonAction:(UIButton *)button {
    [self.moreDelegate clickMoreButton:button];
}

- (void)commentButtonAction:(UIButton *)button {
    [self.commentDelegate clickCommentButton:button];
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
